﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;


namespace OurWedding1.Helpers
{
    public static class GoogleCalendarHelper
    {
        public static MvcHtmlString ExportEventToGoogleLink(this HtmlHelper htmlHelper, string title, DateTime startDateTime, DateTime endDateTime, string description, string location, string linkText, object htmlAttributes = null)
        {
            const string dateFormat = "yyyyMMddTHHmmssZ";
            var url = HttpContext.Current.Request.Url;
            var sb = new StringBuilder();
            sb.Append("http://www.google.com/calendar/event?action=TEMPLATE");
            sb.Append("&text=" + title);
            sb.Append("&dates=" + startDateTime.ToUniversalTime().ToString(dateFormat));
            sb.Append("/");
            sb.Append(endDateTime.ToUniversalTime().ToString(dateFormat));
            sb.Append("&details=" + description);
            sb.Append("&location=" + location);
            sb.Append("&trp=false");
            sb.Append("&sprop=" + url.Scheme + "://" + url.Host);
            sb.Append("&sprop=name:" + url.Host);

            var tagBuilder = new TagBuilder("a");
            tagBuilder.MergeAttribute("href", sb.ToString());
            tagBuilder.MergeAttribute("target", "_blank");
            tagBuilder.InnerHtml = "<span class='event-export-google'>" + linkText + "</span>";
            if (htmlAttributes != null)
                tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
            return MvcHtmlString.Create(tagBuilder.ToString());
        }
    }
}